package com.idp.web.test.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.idp.common.base.BaseController;
import com.idp.common.persistence.Page;
import com.idp.common.util.ValidateUtils;
import com.idp.web.system.entity.SysOrg;
import com.idp.web.test.dao.AccountDao;
import com.idp.web.test.entity.Account;
import com.idp.web.test.entity.Platform;
import com.idp.web.test.service.AccountService;
import com.idp.web.test.service.PlatformService;

import net.sf.json.JSONObject;

/**
 * 
 * ----controller
 * 
 * <pre>
 * 	历史记录：
 * 	2017-06-02 Amber
 * 	新建文件
 * </pre>
 * 
 * @author
 * 
 *         <pre>
 * SD
 * 	Amber
 * PG
 *	Amber
 * UT
 *
 * MA
 *         </pre>
 * 
 * @version $Rev$
 *
 *          <p/>
 *          $Id$
 *
 */
@Controller
@RequestMapping("/account")
public class AccountController extends BaseController {

	private Logger logger = Logger.getLogger(AccountController.class);

	@Resource
	private AccountService accountService;

	@Resource
	private PlatformService platformService;

	@Resource
	private AccountDao accountDao;

	/**
	 * 
	 * <pre>
	 * 	2017-06-02 Amber
	 * 	初始化查询页面
	 * </pre>
	 * 
	 * @return
	 */
	@RequestMapping("/init")
	public String init(HttpServletRequest request) {
		request.setAttribute("platforms", platformService.findBySearch(new Platform()));
		return "shopping/shopmanage/accountSearch";
	}

	/**
	 * 
	 * <pre>
	 * 	2017-06-02 Amber
	 * 	分页查询
	 * </pre>
	 * 
	 * @param account
	 * @param page
	 * @param request
	 * @return
	 */
	@RequestMapping("/list")
	public String list(Account account, Page<Account> page, HttpServletRequest request) {
		if ("全部".equals(account.getUsername())) {
			account.setUsername(null);
		}
		System.out.println(account.getPlatformId() + ": " + account.getUsername());
		try {
			request.setAttribute("page", accountService.findByPage(account, page));
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
		return "shopping/shopmanage/accountList";
	}

	@RequestMapping("/listAccount")
	public String listAccount(Account account, HttpServletRequest request) {

		if ("全部".equals(account.getUsername())) {
			account.setUsername(null);
		}
		System.out.println(account.getPlatformId() + ": " + account.getUsername());
		try {
			List<Account> alist = accountDao.find(account);

			request.setAttribute("alist", alist);

		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
		return "shopping/ordert/ordertSearchEdit";
	}

	/**
	 * 
	 * <pre>
	 * 	2017-06-02 Amber
	 * 	新增修改页面初始化
	 * </pre>
	 * 
	 * @param id
	 * @param request
	 * @return
	 */
	@RequestMapping("/account")
	public String account(Integer id, HttpServletRequest request) {

		try {

			if (ValidateUtils.isNotEmpty(id)) {

				Account account = accountService.getById(id);
				request.setAttribute("account", account);
			}
		} catch (Exception e) {

			logger.error(e.getMessage(), e);
		}

		return "shopping/shopmanage/account";
	}

	/**
	 * 
	 * <pre>
	 * 	2017-06-02 Amber
	 * 	保存
	 * </pre>
	 * 
	 * @param account
	 * @return
	 */
	@RequestMapping("/save")
	@ResponseBody
	public String save(Account account) {

		JSONObject json = new JSONObject();

		try {

			// 修改
			if (ValidateUtils.isNotEmpty(account.getId())) {

				accountService.update(account);
			}
			// 新增
			else {

				accountService.add(account);
			}
			json.put("result", "save_success");
		} catch (Exception e) {

			logger.error(e.getMessage(), e);
			json.put("result", "save_fail");
		}

		return json.toString();
	}

	/**
	 * 
	 * <pre>
	 * 	2017-06-02 Amber
	 * 	删除
	 * </pre>
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping("/delete")
	@ResponseBody
	public String delete(Integer id) {

		JSONObject json = new JSONObject();

		try {
			accountService.delete(id);
			json.put("result", "delete_success");
		} catch (Exception e) {

			logger.error(e.getMessage(), e);
			json.put("result", "delete_fail");
		}

		return json.toString();
	}
}
